﻿using System;
using System.Windows;
using System.Windows.Controls;

namespace LightReader
{
	public partial class Subscribe : UserControl
	{
		public Subscribe()
		{
			// Required to initialize variables
			InitializeComponent();

            feedTitleTextbox.Text = string.Empty;
            feedUrlTextbox.Text = string.Empty;

            setupEventHandlers();
        }

        #region Event Handlers
        public event EventHandler<SubscriptionEventArgs> SubscriptionAdded;
        private void subscribeButton_Click(object sender, RoutedEventArgs e)
        {
            if((feedUrlTextbox.Text.Length > 0) && (feedUrlTextbox.Text.Length > 0)) {
                SubscriptionEventArgs args = new SubscriptionEventArgs();
                args.Title = feedTitleTextbox.Text;
                args.Url = new Uri(feedUrlTextbox.Text);
                OnSubscriptionAdded(args);
            }            
        }

        protected virtual void OnSubscriptionAdded(SubscriptionEventArgs e)
        {
            EventHandler<SubscriptionEventArgs> handler = SubscriptionAdded;
            if (handler != null)
            {
                handler(this, e);
            }
        }
        #endregion

        #region Helper methods
        private void setupEventHandlers()
        {
            subscribeButton.Click += new RoutedEventHandler(subscribeButton_Click);
        }
        #endregion
    }

    // TODO - move
    public class SubscriptionEventArgs : EventArgs
    {
        public string Title { get; set; }
        public Uri Url { get; set; }
    }
}